$(function() {
	var $saveBtn = $('#saveBtn'), $myform = $('#myform');
	
	var options = {
		target: '#success', // target element(s) to be updated with server response 
		beforeSubmit: showRequest, // pre-submit callback, return fals 取消按钮提交（用于校验数据）,icard中失效 (必须是一个函数)
		success: showResponse, // post-submit callback 
		// other available options: 
		//url:       url         // override for form's 'action' attribute 
		//type:      type        // 'get' or 'post', override for form's 'method' attribute 
		//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
//			clearForm: true        // clear all form fields after successful submit 
		resetForm: true        // reset the form after successful submit 

		// $.ajax options can be used here too, for example: 
		//timeout:   3000 
	};
	
	// 1、bind 'myForm' and provide a simple callback function
	// Prepares a form to be submitted via AJAX by adding all of the necessary event listeners
	// 提交按钮必须为submit类型，提交整个表单
	$myform.ajaxForm(function(data) {
        console.log(data);
    });
	
	// 2、attach handler to form's submit event
	//Immediately submits the form via AJAX
	$myform.submit(function() {
	    // submit the form
	    $(this).ajaxSubmit(function() {
	    	alert('success');
	    });
	    // return false to prevent normal browser submit and page navigation
	    return false;
	});
	
	//3、按钮不为submit类型，去掉e.preventDefault()
	//方法里面给option添加属性，是否是全局的? TODO
	$saveBtn.click(function(e) {
		e.preventDefault();//
		$myform.ajaxSubmit(options);
	});
});

function showRequest() {
	return true;
};

function showResponse() {
	alert('showResponse');
}